/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.bank.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.tool.MyStringUtil;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.bank.service.ITmCustomerCallRecordService;
import org.springblade.modules.bank.vo.TaskReportVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 报表查询
 *
 * @author Blade
 * @since 2022-07-08
 */
@RestController
@AllArgsConstructor
@Api(value = "报表查询", tags = "报表查询")
@RequestMapping("/reportcontroller")
public class ReportController extends BladeController {

	private ITmCustomerCallRecordService tmCustomerCallRecordService;

	/**
	 * 统计客户经理完成率
	 */
	@GetMapping("/getTaskCount")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "tmVisitTaskCustomerVO")
	public R getTaskCount(TaskReportVO taskReportVO) {
		String sql = "";
		String condition = "";
		if ("branchQuery".equals(taskReportVO.getReportType())){
			String taskInfo = "tm_visit_task";
			//taskType为空 查询的是已完成的，不需要主任务时间条件查询
			String deptInfo = "";
			if (MyStringUtil.isEmpty(taskReportVO.getTaskType())){
				if (MyStringUtil.isNotEmpty(taskReportVO.getDataPermission())){
					if (taskReportVO.getDataPermission()==1){
						deptInfo = "(SELECT id,dept_name from blade_dept where is_deleted=0 )";
					}else if(taskReportVO.getDataPermission()==2){
						deptInfo = "(SELECT id,dept_name from blade_dept where is_deleted=0 and parent_id="+taskReportVO.getQueryParentDeptId()+")";
						taskInfo = "(select * from tm_visit_task where dept_id in (select id from blade_dept t where t.parent_id="+taskReportVO.getQueryParentDeptId()+") )";
					}else if(taskReportVO.getDataPermission()==3){
						deptInfo = "(SELECT id,dept_name from blade_dept where is_deleted=0 and id="+taskReportVO.getQueryDeptId()+")";
						taskInfo = "(select * from tm_visit_task where dept_id = '"+taskReportVO.getQueryDeptId()+"')";
					}
				}
			}else{
				//taskType不为空 查询的是1商机任务  2日常任务 所有的，需要主任务时间条件查询
				if (MyStringUtil.isNotEmpty(taskReportVO.getDataPermission())){
					String timeCondition = "1=1";
					if (MyStringUtil.isNotEmpty(taskReportVO.getMonth())){
						String[] date = taskReportVO.getMonth().split(",");
						timeCondition="(date(task_begin_date) between date('"+date[0]+"') and date('"+date[1]+"'))";
					}else{
						timeCondition="(date(SYSDATE()) BETWEEN task_begin_date and task_end_date)";
					}
					String ex1Condition = "1=1";
					if ("RC".equals(taskReportVO.getTaskType())){
						ex1Condition = "ex1='2'";
					}else if ("SJ".equals(taskReportVO.getTaskType())){
						ex1Condition = "ex1='1' and task_status = '4'";
						if (MyStringUtil.isNotEmpty(taskReportVO.getTaskId())){
							ex1Condition += " and id="+taskReportVO.getTaskId();
						}
					}
					if (taskReportVO.getDataPermission()==1){
						taskInfo = "(select * from tm_visit_task where "+timeCondition+" and "+ex1Condition+ ")";
					}else if(taskReportVO.getDataPermission()==2){
						taskInfo = "(select * from tm_visit_task where "+timeCondition+" and "+ex1Condition+" and dept_id in (select id from blade_dept t where t.parent_id="+taskReportVO.getQueryParentDeptId()+") )";
					}else if(taskReportVO.getDataPermission()==3){
						taskInfo = "(select * from tm_visit_task where "+timeCondition+" and "+ex1Condition+" and dept_id = '"+taskReportVO.getQueryDeptId()+"')";
					}
				}
			}

			condition = "1=1";
			//为空则为查询完成的
			if (MyStringUtil.isEmpty(taskReportVO.getTaskType())){
				if (MyStringUtil.isNotEmpty(taskReportVO.getMonth())){
					String[] date = taskReportVO.getMonth().split(",");
					condition += " and (date(t.visit_time) between date('"+date[0]+"') and date('"+date[1]+"'))";
				}else{
					condition += " and (date(SYSDATE()) BETWEEN task_begin_date and task_end_date)";
				}
			}

			if ("RC".equals(taskReportVO.getTaskType())){
				sql = "with a as \n" +
						"(select task.id,task.dept_id,task.dept_name,task.task_code,task.task_name,task.task_begin_date,task.task_end_date,COUNT(t.id) ct,\n" +
						"sum(case when t.visit_status='0' then 1 else 0 end) wwc,\n" +
						"sum(case when t.visit_status='1' then 1 else 0 end) ywc,\n" +
						"sum(case when t.check_status='0' then 1 else 0 end) ytg,\n" +
						"sum(case when t.check_status='1' then 1 else 0 end) tg,\n" +
						"sum(case when t.check_status='-1' then 1 else 0 end) wtg\n" +
						"from "+taskInfo+" task left join tm_visit_task_customer t on task.id=t.task_id where "+condition+" \n" +
						" group by task.id,task.dept_id,task.dept_name,task.task_code,task.task_name,task.task_begin_date,task.task_end_date)\n" +
						"select CONCAT(a.id,'') id,a.dept_id,a.dept_name,a.task_code,a.task_name,a.task_begin_date,a.task_end_date,a.ct,a.ywc,a.wwc,round(a.ywc/a.ct*100,2) fgl,a.ytg,a.tg,a.wtg,concat(round((a.ytg+a.tg)/a.ct*100,2),'%') ytgl from a order by a.dept_id;";
			}else if ("SJ".equals(taskReportVO.getTaskType())){
				sql = "with a as \n" +
						"(select task.id,task.dept_id,task.dept_name,task.task_code,task.task_name,task.task_begin_date,task.task_end_date,task.cust_number,COUNT(t.id) ct,\n" +
						"sum(case when t.visit_status='0' then 1 else 0 end) wwc,\n" +
						"sum(case when t.visit_status='1' then 1 else 0 end) ywc,\n" +
						"sum(case when t.check_status='0' then 1 else 0 end) ytg,\n" +
						"sum(case when t.check_status='1' then 1 else 0 end) tg,\n" +
						"sum(case when t.check_status='-1' then 1 else 0 end) wtg\n" +
						"from "+taskInfo+" task left join tm_visit_task_customer t on task.id=t.task_id where "+condition+" \n" +
						" group by task.id,task.dept_id,task.dept_name,task.task_code,task.task_name,task.task_begin_date,task.task_end_date,task.cust_number)\n" +
						"select CONCAT(a.id,'') id,a.dept_id,a.dept_name,a.task_code,a.task_name,a.task_begin_date,a.task_end_date,a.cust_number,a.ct,a.ywc,a.wwc,round(a.ywc/a.ct*100,2) fgl,a.ytg,a.tg,a.wtg,concat(round((a.ytg+a.tg)/a.ct*100,2),'%') ytgl from a order by a.dept_id;";
			}else{
				sql = "with a as \n" +
						"(select t.dept_id,t.dept_name,COUNT(t.id) ct,\n" +
						"sum(case when t.visit_status='0' then 1 else 0 end) wwc,\n" +
						"sum(case when t.visit_status='1' then 1 else 0 end) ywc,\n" +
						"sum(case when t.check_status='0' then 1 else 0 end) ytg,\n" +
						"sum(case when t.check_status='1' then 1 else 0 end) tg,\n" +
						"sum(case when t.check_status='-1' then 1 else 0 end) wtg\n" +
						"from "+taskInfo+" task left join tm_visit_task_customer t on task.id=t.task_id where "+condition+" \n" +
						" and t.visit_status='1' group by t.dept_id,t.dept_name)\n" +
						"select dept.id dept_id,dept.dept_name,ifnull(a.ct,0) ct,ifnull(a.ywc,0) ywc,ifnull(a.wwc,0) wwc,round(a.ywc/a.ct*100,2) fgl,ifnull(a.ytg,0) ytg,ifnull(a.tg,0) tg,ifnull(a.wtg,0) wtg,concat(round((a.ytg+a.tg)/a.ct*100,2),'%') ytgl from " +
						""+deptInfo+" dept left join a on dept.id=a.dept_id order by a.dept_id;";
			}
		}
		if ("branchMgrQuery".equals(taskReportVO.getReportType())){
			//已完成的任务查询
			if (MyStringUtil.isEmpty(taskReportVO.getTaskType())){
				condition = "1=1";
				if (MyStringUtil.isNotEmpty(taskReportVO.getMonth())){
					String[] date = taskReportVO.getMonth().split(",");
					condition += " and (date(t.visit_time) between date('"+date[0]+"') and date('"+date[1]+"'))";
				}else{
					condition += " and (date(SYSDATE()) BETWEEN task_begin_date and task_end_date)";
				}
				if (MyStringUtil.isNotEmpty(taskReportVO.getCustMgr())){
					condition += " and t.cust_mgr="+taskReportVO.getCustMgr();
				}
				String taskInfo = "tm_visit_task";
				if (MyStringUtil.isNotEmpty(taskReportVO.getDeptId())){
					taskInfo = "(select * from tm_visit_task where dept_id = '"+taskReportVO.getDeptId()+"')";
				}
				sql = "with a as \n" +
						"(select t.dept_id,t.dept_name,t.cust_mgr,t.cust_mgr_name,COUNT(t.id) ct,\n" +
						"sum(case when t.visit_status='0' then 1 else 0 end) wwc,\n" +
						"sum(case when t.visit_status='1' then 1 else 0 end) ywc,\n" +
						"sum(case when t.check_status='0' then 1 else 0 end) ytg,\n" +
						"sum(case when t.check_status='1' then 1 else 0 end) tg,\n" +
						"sum(case when t.check_status='-1' then 1 else 0 end) wtg\n" +
						"from "+taskInfo+" task left join tm_visit_task_customer t on task.id=t.task_id where "+condition+" \n" +
						" and t.visit_status='1' group by t.dept_id,t.dept_name,t.cust_mgr,t.cust_mgr_name)\n" +
						"select a.dept_id,a.dept_name,a.cust_mgr,a.cust_mgr_name,a.ct,a.ywc,a.wwc,round(a.ywc/a.ct*100,2) fgl,a.ytg,a.tg,a.wtg,concat(round((a.ytg+a.tg)/a.ct*100,2),'%') ytgl from a order by a.cust_mgr;";
			}else{
				condition = "1=1";
				if ("RC".equals(taskReportVO.getTaskType())){
					//管户任务
					String taskInfo = "(select * from tm_visit_task where ex1='2')";
					if (MyStringUtil.isNotEmpty(taskReportVO.getTaskId())){
						taskInfo = "(select * from tm_visit_task where id = "+taskReportVO.getTaskId()+")";
					}else{
						if (MyStringUtil.isNotEmpty(taskReportVO.getMonth())){
							String[] date = taskReportVO.getMonth().split(",");
							taskInfo = "(select * from tm_visit_task where (date(task_begin_date) between date('"+date[0]+"') and date('"+date[1]+"')) and ex1='2')";
						}else{
							taskInfo = "(select * from tm_visit_task where (date(SYSDATE()) BETWEEN task_begin_date and task_end_date) and ex1='2')";
						}
						if (MyStringUtil.isNotEmpty(taskReportVO.getCustMgr())){
							condition += " and t.cust_mgr="+taskReportVO.getCustMgr();
						}
					}
					sql = "with a as \n" +
							"(select task.id,task.task_code,task.task_name,task.dept_id,task.dept_name,task.task_begin_date,task.task_end_date,t.cust_mgr,t.cust_mgr_name,COUNT(t.id) ct,\n" +
							"sum(case when t.visit_status='0' then 1 else 0 end) wwc,\n" +
							"sum(case when t.visit_status='1' then 1 else 0 end) ywc,\n" +
							"sum(case when t.check_status='0' then 1 else 0 end) ytg,\n" +
							"sum(case when t.check_status='1' then 1 else 0 end) tg,\n" +
							"sum(case when t.check_status='-1' then 1 else 0 end) wtg\n" +
							"from "+taskInfo+" task" +
							" left join tm_visit_task_customer t on task.id=t.task_id where " +condition+
							" group by task.id,task.task_code,task.task_name,task.dept_id,task.dept_name,task.task_begin_date,task.task_end_date,t.cust_mgr,t.cust_mgr_name)\n" +
							"select CONCAT(a.id,'') id,a.task_code,a.task_name,a.dept_id,a.dept_name,a.task_begin_date,a.task_end_date,a.cust_mgr,a.cust_mgr_name" +
							",a.ct,a.ywc,a.wwc,round(a.ywc/a.ct*100,2) fgl,a.ytg,a.tg,a.wtg,concat(round((a.ytg+a.tg)/a.ct*100,2),'%') ytgl " +
							" from a  order by a.cust_mgr";
				}else if ("SJ".equals(taskReportVO.getTaskType())){
					//商机任务
					String taskInfo = "(select * from tm_visit_task where ex1='1' and task_status = '4')";
					String mgrCondition = "";
					if (MyStringUtil.isNotEmpty(taskReportVO.getTaskId())){
						taskInfo = "(select * from tm_visit_task where id = "+taskReportVO.getTaskId()+")";
					}else{
						if (MyStringUtil.isNotEmpty(taskReportVO.getMonth())){
							String[] date = taskReportVO.getMonth().split(",");
							taskInfo = "(select * from tm_visit_task where (date(task_begin_date) between date('"+date[0]+"') and date('"+date[1]+"')) and ex1='1' and task_status = '4')";
						}else{
							taskInfo = "(select * from tm_visit_task where (date(SYSDATE()) BETWEEN task_begin_date and task_end_date) and ex1='1' and task_status = '4')";
						}
						if (MyStringUtil.isNotEmpty(taskReportVO.getCustMgr())){
							condition += " and t.cust_mgr="+taskReportVO.getCustMgr();
							mgrCondition = " and mgr.cust_mgr="+taskReportVO.getCustMgr();
						}
					}
					sql = "with a as \n" +
							"(select task.id,task.dept_id,task.dept_name,t.cust_mgr,t.cust_mgr_name,COUNT(t.id) ct,\n" +
							"sum(case when t.visit_status='0' then 1 else 0 end) wwc,\n" +
							"sum(case when t.visit_status='1' then 1 else 0 end) ywc,\n" +
							"sum(case when t.check_status='0' then 1 else 0 end) ytg,\n" +
							"sum(case when t.check_status='1' then 1 else 0 end) tg,\n" +
							"sum(case when t.check_status='-1' then 1 else 0 end) wtg\n" +
							"from "+taskInfo+" task" +
							" left join tm_visit_task_customer t on task.id=t.task_id where " +condition+
							" group by task.id,task.dept_id,task.dept_name,t.cust_mgr,t.cust_mgr_name)\n" +
							"select CONCAT(ta.id,'') id,ta.task_code,ta.task_name,ta.dept_id,ta.dept_name,ta.task_begin_date,ta.task_end_date,mgr.cust_mgr,mgr.cust_mgr_name,mgr.cust_number_plan" +
							",a.ct,a.ywc,a.wwc,round(a.ywc/a.ct*100,2) fgl,a.ytg,a.tg,a.wtg,concat(round((a.ytg+a.tg)/a.ct*100,2),'%') ytgl " +
							" from "+taskInfo+" ta left join tm_visit_task_cust_mgr mgr on ta.id=mgr.dept_task_id " +
							" left join a on mgr.dept_task_id=a.id and mgr.cust_mgr = a.cust_mgr" +
							" where 1=1" + mgrCondition +
							" order by mgr.cust_mgr";
				}
			}


//			扩展字段1  1商机任务  2日常任务
			if ("RC".equals(taskReportVO.getTaskType())){
				condition = "task.ex1='2'";
			}else if ("SJ".equals(taskReportVO.getTaskType())){
				condition = "task.ex1='1'";
				if (MyStringUtil.isNotEmpty(taskReportVO.getTaskId())){
					condition += " and task.id="+taskReportVO.getTaskId();
				}
			}else{
				condition = "1=1";
			}


		}
		List list = tmCustomerCallRecordService.queryBySql(sql);
		return R.data(list);
	}

	/**
	 * 完成户数统计
	 */
	@GetMapping("/getVistTaskCount")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "tmVisitTaskCustomerVO")
	public R getVistTaskCount(TaskReportVO taskReportVO) {
		String[] date = taskReportVO.getMonth().split(",");
		String record = "tm_customer_call_record";
		if (MyStringUtil.isNotEmpty(taskReportVO.getDataPermission())&&"branchQuery".equals(taskReportVO.getReportType())){
			if (taskReportVO.getDataPermission()==1){
				record = "(	select a.dept_id,0 jhnkh,count(a.customer_code) jhwkh from \n" +
						"     (select distinct customer_code,dept_id,aa.call_date\n" +
						"	    from tm_customer_call_record aa where aa.call_date between date('"+date[0]+"') and date('"+date[1]+"')\n" +
						"        and (aa.hangup_status ='0' or aa.hangup_status ='1' or aa.hangup_status ='4') and length(task_id)<=6" +
						"		) a\n" +
						"	group by a.dept_id\n" +
						"	union all" +
						"	select aa.dept_id,count(customer_code) jhnkh,0 jhwkh from tm_visit_task_customer aa\n" +
						"		where visit_status='1' and (visit_time between timestamp('"+date[0]+"') and timestamp(DATE_ADD('"+date[1]+"',INTERVAL 1 DAY)))\n" +
						"   group by aa.dept_id)";
			}else if(taskReportVO.getDataPermission()==2){
				record = "(	select a.dept_id,0 jhnkh,count(a.customer_code) jhwkh from \n" +
						"     (select distinct customer_code,dept_id,aa.call_date\n" +
						"	    from tm_customer_call_record aa where aa.call_date between date('"+date[0]+"') and date('"+date[1]+"')\n" +
						"        and (aa.hangup_status ='0' or aa.hangup_status ='1' or aa.hangup_status ='4') and length(task_id)<=6" +
						" 		 and dept_id in (select id from blade_dept t where t.parent_id="+taskReportVO.getQueryParentDeptId()+") "+
						"		) a\n" +
						"	group by a.dept_id\n" +
						"	union all" +
						"	select aa.dept_id,count(customer_code) jhnkh,0 jhwkh from tm_visit_task_customer aa\n" +
						"		where visit_status='1' and (visit_time between timestamp('"+date[0]+"') and timestamp(DATE_ADD('"+date[1]+"',INTERVAL 1 DAY)))\n" +
						"		and dept_id in (select id from blade_dept t where t.parent_id="+taskReportVO.getQueryParentDeptId()+") " +
						"   group by aa.dept_id)";
			}else if(taskReportVO.getDataPermission()==3){
				record = "(	select a.dept_id,0 jhnkh,count(a.customer_code) jhwkh from \n" +
						"     (select distinct customer_code,dept_id,aa.call_date\n" +
						"	    from tm_customer_call_record aa where aa.call_date between date('"+date[0]+"') and date('"+date[1]+"')\n" +
						"        and (aa.hangup_status ='0' or aa.hangup_status ='1' or aa.hangup_status ='4') and length(task_id)<=6" +
						" 		 and dept_id = '"+taskReportVO.getQueryDeptId()+"'"+
						"		) a\n" +
						"	group by a.dept_id\n" +
						"	union all" +
						"	select aa.dept_id,count(customer_code) jhnkh,0 jhwkh from tm_visit_task_customer aa\n" +
						"		where visit_status='1' and (visit_time between timestamp('"+date[0]+"') and timestamp(DATE_ADD('"+date[1]+"',INTERVAL 1 DAY)))\n" +
						" 		 and dept_id = '"+taskReportVO.getQueryDeptId()+"'"+
						"   group by aa.dept_id)";
			}
		}
		String sql = "";
		if ("branchQuery".equals(taskReportVO.getReportType())){
			sql = " select bb.dept_id,cc.dept_name,sum(jhnkh)+sum(jhwkh) zkh,sum(jhnkh) jhnkh,sum(jhwkh) jhwkh\n" +
					"    from "+record+"  bb left join  blade_dept  cc on bb.dept_id =cc.id" +
					"		 group by bb.dept_id,cc.dept_name order by  bb.dept_id";
		}
		if ("branchMgrQuery".equals(taskReportVO.getReportType())){
			sql = " select bb.dept_id,cc.dept_name,bb.code,dd.name, sum(jhnkh)+sum(jhwkh) zkh,sum(jhnkh) jhnkh,sum(jhwkh) jhwkh\n" +
					"    from (select a.dept_id,a.code,0 jhnkh,count(a.customer_code) jhwkh from \n" +
					"     (select distinct customer_code,dept_id, aa.cust_mgr code,aa.call_date\n" +
					"      from tm_customer_call_record aa \n" +
					"     where aa.call_date between  date('"+date[0]+"') and date('"+date[1]+"')\n" +
					"            and (aa.hangup_status ='0' or aa.hangup_status ='1'  or aa.hangup_status ='4')            and length(task_id)<=6 and aa.dept_id='"+taskReportVO.getQueryDeptId()+"' ) a\n" +
					"		group by a.dept_id,a.code\n" +
					"	    union all\n" +
					"       select aa.dept_id,aa.cust_mgr code,count(customer_code) jhnkh,0 jhwkh \n" +
					"		 from   tm_visit_task_customer aa\n" +
					"       where visit_status='1' \n" +
					"       and (visit_time between timestamp('"+date[0]+"') and timestamp(DATE_ADD('"+date[1]+"',INTERVAL 1 DAY))) and  aa.dept_id='"+taskReportVO.getQueryDeptId()+"' \n" +
					"        group by aa.dept_id,aa.cust_mgr )  bb \n" +
					"		left join  blade_dept  cc on bb.dept_id =cc.id\n" +
					"		left join blade_user dd on bb.code =dd.code\n" +
					"		group by bb.dept_id,cc.dept_name,bb.code,dd.name order by  bb.code  ";
		}
		if ("employee".equals(taskReportVO.getReportType())){
			sql = " select bb.code cust_mgr,cc.name,bb.customer_code,bb.lx,bb.call_date\n" +
					"    from (select a.code,a.customer_code, '计划外' lx,a.call_date from \n" +
					"     (select distinct customer_code, aa.cust_mgr code,aa.call_date\n" +
					"	     from tm_customer_call_record aa \n" +
					"     where aa.call_date between  date('"+date[0]+"') and date('"+date[1]+"')\n" +
					"            and (aa.hangup_status ='0' or aa.hangup_status ='1'  or aa.hangup_status ='4') and length(task_id)<=6 and aa.cust_mgr='"+taskReportVO.getCustMgr()+"' ) a\n" +
					"     union all \n" +
					"      select aa.cust_mgr code,customer_code, '计划内' lx,date(aa.visit_time)  call_date\n" +
					"  from   tm_visit_task_customer aa\n" +
					"       where visit_status='1' \n" +
					"       and (visit_time between timestamp('"+date[0]+"') and timestamp(DATE_ADD('"+date[1]+"',INTERVAL 1 DAY))) and aa.cust_mgr='"+taskReportVO.getCustMgr()+"' )  bb \n" +
					"		left join blade_user cc on bb.code =cc.code\n" +
					"		order by bb.call_date desc";
		}
		List list = tmCustomerCallRecordService.queryBySql(sql);
		return R.data(list);
	}


	@GetMapping("/getUserRecord")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "taskReportVO")
	public R getUserRecord(TaskReportVO taskReportVO) {
		String sql = "";
		if (taskReportVO.getDataPermission()==1){
			sql = "select *,b.dept_name from blade_user a left join blade_dept b on a.dept_id=b.id where 1=1 order by a.dept_id,a.code";
		}else if(taskReportVO.getDataPermission()==2){
			sql = "select *,b.dept_name from blade_user a left join blade_dept b on a.dept_id=b.id where 1=1 and dept_id in (select id from blade_dept t where t.parent_id="+taskReportVO.getQueryParentDeptId()+") order by a.dept_id,a.code";
		}else if(taskReportVO.getDataPermission()==3){
			sql = "select *,b.dept_name from blade_user a left join blade_dept b on a.dept_id=b.id where 1=1 and dept_id = '"+taskReportVO.getQueryDeptId()+"' order by a.dept_id,a.code";
		}
		List list = tmCustomerCallRecordService.queryBySql(sql);
		return R.data(list);
	}
}
